package com.gca.airhockey;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import android.content.Context;

public class Paddle implements IPhysicalObject{
	// ===========================================================
	// Constants
	// ===========================================================
	private static final String PADDLE_IMAGE_RED = "gfx/paddle_red.png";
	private static final String PADDLE_IMAGE_BLUE = "gfx/paddle_blue.png";
	private static final String PADDLE_IMAGE_GREEN = "gfx/paddle_green.png";
	private static final String PADDLE_IMAGE_YELLOW = "gfx/paddle_yellow.png";

	private static final int POS_X = 0;
	private static final int POS_Y = 0;

	private BitmapTextureAtlas mTexture = null;
	private TextureRegion mRegion = null;
	private Body mBody;
	private Sprite mSprite;
	
	private float xDeplacement;
	private float yDeplacement;

	// ===========================================================
	// Constructors
	// ===========================================================
	public Paddle(Context pContext, Engine engine,PhysicsWorld physicsWorld, EColor color) {
		
		this.mTexture = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		String fichierImage;
		
		switch (color) {
		case RED:
			fichierImage = PADDLE_IMAGE_RED;
			break;
		case BLUE:
			fichierImage = PADDLE_IMAGE_BLUE;
			break;
		case GREEN:
			fichierImage = PADDLE_IMAGE_GREEN;
			break;
		case YELLOW:
			fichierImage = PADDLE_IMAGE_YELLOW;
			break;
		default:
			fichierImage = PADDLE_IMAGE_RED;
			break;
		}
		
		this.mRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, pContext, fichierImage, 0, 0);
		engine.getTextureManager().loadTexture(this.mTexture);
		this.mSprite = new Sprite(POS_X, POS_Y, mRegion);
		
		this.mSprite.setScale(0.5f);
		
		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.3f);
		
		
		mBody = PhysicsFactory.createCircleBody(physicsWorld, mSprite, BodyType.DynamicBody, objectFixtureDef);
		mBody.setUserData(this);
		physicsWorld.registerPhysicsConnector(new PhysicsConnector(mSprite, mBody, true, true));
		
		mBody.setActive(false);
		mSprite.setVisible(false);
	}

	// ===========================================================
	// Getters and Setters
	// ===========================================================
	public Sprite getSprite() {
		return mSprite;
	}
	public Body getBody() {
		return mBody;
	}
	public float getXDeplacement()
	{
		return xDeplacement;
	}
	public float getYDeplacement()
	{
		return yDeplacement;
	}
	public void setXDeplacement(float value)
	{
		xDeplacement = value;
	}
	public void setYDeplacement(float value)
	{
		yDeplacement = value;
	}
	
	@Override
	public String toString() {
		return "Paddle";
	}
}
